<?php

/**
 * News form base class.
 *
 * @package    elearning
 * @subpackage form
 * @author     Your name here
 */
class BaseNewsForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'               => new sfWidgetFormInputHidden(),
      'news_category_id' => new sfWidgetFormPropelChoice(array('model' => 'NewsCategory', 'add_empty' => true)),
      'thumb'            => new sfWidgetFormInput(),
      'ordering'         => new sfWidgetFormInput(),
      'is_public'        => new sfWidgetFormInput(),
      'expires_at'       => new sfWidgetFormDateTime(),
      'created_at'       => new sfWidgetFormDateTime(),
      'updated_at'       => new sfWidgetFormDateTime(),
      'news_tags_list'   => new sfWidgetFormPropelChoiceMany(array('model' => 'Tags')),
    ));

    $this->setValidators(array(
      'id'               => new sfValidatorPropelChoice(array('model' => 'News', 'column' => 'id', 'required' => false)),
      'news_category_id' => new sfValidatorPropelChoice(array('model' => 'NewsCategory', 'column' => 'id', 'required' => false)),
      'thumb'            => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'ordering'         => new sfValidatorInteger(array('required' => false)),
      'is_public'        => new sfValidatorInteger(array('required' => false)),
      'expires_at'       => new sfValidatorDateTime(array('required' => false)),
      'created_at'       => new sfValidatorDateTime(array('required' => false)),
      'updated_at'       => new sfValidatorDateTime(array('required' => false)),
      'news_tags_list'   => new sfValidatorPropelChoiceMany(array('model' => 'Tags', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('news[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'News';
  }

  public function getI18nModelName()
  {
    return 'NewsI18n';
  }

  public function getI18nFormClass()
  {
    return 'NewsI18nForm';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['news_tags_list']))
    {
      $values = array();
      foreach ($this->object->getNewsTagss() as $obj)
      {
        $values[] = $obj->getTagsId();
      }

      $this->setDefault('news_tags_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveNewsTagsList($con);
  }

  public function saveNewsTagsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['news_tags_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(NewsTagsPeer::NEWS_ID, $this->object->getPrimaryKey());
    NewsTagsPeer::doDelete($c, $con);

    $values = $this->getValue('news_tags_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new NewsTags();
        $obj->setNewsId($this->object->getPrimaryKey());
        $obj->setTagsId($value);
        $obj->save();
      }
    }
  }

}
